<?php
namespace app\common\model;
use app\common\model\Express as Expressmodel;
use think\facade\Cache;
use app\common\exception\BaseException;
use think\facade\Request;
use think\db;
/**
 * 系统设置模型
 * Class Setting
 * @package app\common\model
 */
class Columnad extends BaseModel
{
    protected $name = 'columnad';
		/**
     * 设置项描述
     * @var array
     */
    private $describe = [
		'store' => '商城设置',
		'trade' => '交易设置',
		'storage' => '上传设置',
		'sms' => '短信通知',
		'tplMsg' => '模板消息',
        'basic' => '基础设置',
        'condition' => '分销商条件',
		'jifen'=>'签到积分',
        'commission' => '佣金设置',
        'settlement' => '结算',
        'words' => '自定义文字',
        'license' => '申请协议',
        'background' => '页面背景图',
        'template_msg' => '模板消息',
        'qrcode' => '分销海报',
		 'market' => '营销设置',
    ];
	   /**
     * 设置项描述
     * @var array
     */
	public $group_list = array(
		'store' => '网站信息',
		'basic' => '基本设置',
		'sms' => '短信设置',
		'shopping' => '购物流程设置',
		'smtp' => '邮件设置',
		'water' => '水印设置',
		'distribut' => '分销设置'
	);
    /**
     * 获取器: 转义数组格式
     * @param $value
     * @return mixed
     */
    public function getValuesAttr($value)
    {
        return json_decode($value, true);
    }
    /**
     * 修改器: 转义成json格式
     * @param $value
     * @return string
     */
    public function setValuesAttr($value)
    {
        return json_encode($value);
    }


    /**
     * 获取列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        return $this
            ->order(['vip' => 'asc'])
            ->paginate(15, false, [
                'query' => Request::instance()->request()
            ]);
    }

    /**
     * 获取下级列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getZiList($data)
    {
        return $this
            ->where('column_pid','=',$data)
            ->order(['vip' => 'asc'])
            ->paginate(15, false, [
                'query' => Request::instance()->request()
            ]);
    }


    /**
     * 添加新记录
     * @param $data
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function addcolumnad($data)
    {
        if (!isset($data['name']) || empty($data['name'])) {
            $this->error = '请选择分类名称';
            return false;
        }
        $save = [
            'column_name'=>$data['name'],
            'vip' =>$data['vip'],
            'create_time' => time(),
            'update_time' => time()
        ];

        return $this->save($save);
    }

    /**
     * 添加子记录
     * @param $data
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function addZicolumnad($data,$column_pid)
    {
        if (!isset($data['name']) || empty($data['name'])) {
            $this->error = '请选择分类名称';
            return false;
        }
        $save = [
            'column_name'=>$data['name'],
            'vip' =>$data['vip'],
            'column_pid' =>$column_pid,
            'create_time' => time(),
            'update_time' => time()
        ];

        return $this->save($save);
    }

    /**
     * 多少子类
     * @param $data
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function zileisum($data)
    {
        if (!isset($data['column_pid']) || empty($data['column_pid'])) {
            $this->error = '请选择分类';
            return false;
        }
        return $this->where('column_pid','=',$data['column_pid'])->count();
    }

    /**
     * 多删除
     * @param $data
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function columnaddelete($data)
    {
        if (!isset($data) || empty($data)) {
            $this->error = '请选择分类';
            return false;
        }
        return $this->where('column_id','=',$data)->delete();
    }

    /**
     * 单个
     * @param $data
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function columnadfind($data)
    {
        if (!isset($data) || empty($data)) {
            $this->error = '请选择分类';
            return false;
        }
        return $this->where('column_id','=',$data)->find();
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function columnadedit($data) {

        $save = [
            'column_name'=>$data['name'],
            'vip' =>$data['vip'],
            'update_time' => time()
        ];

        return $this->where('column_id',$data['column_id'])->update($save);
    }



}